package com.example.administrator.szb.fragments.fragment_forMy;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.bumptech.glide.Glide;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.GWCententActivity;
import com.example.administrator.szb.adapter.BaseAdapter;
import com.example.administrator.szb.fragments.base.BaseFragment;
import com.example.administrator.szb.bean.GZBean;
import com.example.administrator.szb.bean.MyGZBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;
import com.example.administrator.szb.util.Toasts;

import java.util.HashMap;

import butterknife.Bind;
import butterknife.ButterKnife;

/**
 * Created by D N on 2017/11/6.
 */

public class FragmentGZGL extends BaseFragment {
    View view;
    String url;
    int type;//1,代表我的关注，2，代表关注我的
    @Bind(R.id.wdgl_fragment)
    RecyclerView wdglFragment;
    private int current_page = 1;//列表当前页
    private int per_page = 10;//默认展示条数
    BaseAdapter<MyGZBean.DataBean> adapter;
    MyGZBean bean;
    View error_head;
    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        type = getArguments().getInt("type");
        if (type == 1) {
            url = URLAddress.URLBASE + URLAddress.URL_MYGZ;
        } else if (type == 2) {
            url = URLAddress.URLBASE + URLAddress.URL_GZMY;
        }
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.wdgl_fragment, container, false);
        requestData();
        ButterKnife.bind(this, view);
        return view;
    }

    private void requestData() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("id", SampleApplicationLike.getUserloginInstance().getId() + "");
        map.put("current_page", current_page + "");
        map.put("per_page", per_page + "");
        HttpUtils.post2(activity,url, map, 0, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                try {
                    bean = JSON.parseObject(str,new TypeReference<MyGZBean>(){});
                } catch (Exception e) {
                    str = str.replace("\"data\":{}","\"data\":[]");
                    bean = JSON.parseObject(str,new TypeReference<MyGZBean>(){});
                }
                if (bean.getResult() == 1) {
                    initRecyclerview();
                }
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });

    }

    private void initRecyclerview() {
        wdglFragment.setLayoutManager(new LinearLayoutManager(getActivity()));
        adapter = new BaseAdapter<MyGZBean.DataBean>(getActivity(),bean.getData()) {
            @Override
            public void convert(int position, final BaseViewHolder holder, final MyGZBean.DataBean dataBean) {
                ((TextView)holder.getView(R.id.textView54)).setText(dataBean.getName());
                if (dataBean.getReal_name()!=null&&dataBean.getReal_name().size()>0&&dataBean.getReal_name().get(0).length()>0){
                    //String arr[] = dataBean.getReal_name().split("-");
                    ((TextView)holder.getView(R.id.textView54)).setText(dataBean.getName()+"("+dataBean.getReal_name().get(0)+"经理)");
                }
                if (type == 2){
                    ((CheckBox)holder.getView(R.id.checkBox2)).setVisibility(View.GONE);
                }else {
                    final CheckBox checkBox = (CheckBox)holder.getView(R.id.checkBox2);
                    checkBox.setVisibility(View.VISIBLE);
                    checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                        @Override
                        public void onCheckedChanged(CompoundButton compoundButton, boolean b) {
                            if (b){
                                checkBox.setText("关注");
                                checkBox.setTextColor(getResources().getColor(R.color.basecolor2));
                                checkBox.setBackground(getResources().getDrawable(R.drawable.border_content2));
                                doGZOrNOt(dataBean.getId());
                            }else{
                                checkBox.setText("取消关注");
                                checkBox.setTextColor(getResources().getColor(R.color.gray1));
                                checkBox.setBackground(getResources().getDrawable(R.drawable.border_content3));
                                doGZOrNOt(dataBean.getId());
                            }

                        }
                    });
                }

                if (dataBean.getType() == 2){
                    (holder.getView(R.id.textView67)).setVisibility(View.VISIBLE);
                    (holder.getView(R.id.textView165)).setVisibility(View.GONE);
                }else{
                    (holder.getView(R.id.textView67)).setVisibility(View.GONE);
                    (holder.getView(R.id.textView165)).setVisibility(View.VISIBLE);
                    ((TextView)holder.getView(R.id.textView165)).setText((String)dataBean.getSketch()+"");
                }

                if (!TextUtils.isEmpty(dataBean.getHeadimg())){
                    Glide.with(getActivity()).load(dataBean.getHeadimg()).into((ImageView) holder.getView(R.id.imageView26));
                }



            }

            @Override
            public int getLayoutId(int viewType) {
                return R.layout.view_mygz_item;
            }
        };


        adapter.setOnItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, int position) {
                Intent intent_go = new Intent(getActivity(), GWCententActivity.class);
                intent_go.putExtra("refer_id", bean.getData().get(position).getId());
                startActivity(intent_go);
            }
        });

        if (bean.getData().size() == 0){
            error_head = LayoutInflater.from(getActivity()).inflate(R.layout.view_item_error, wdglFragment, false);
            adapter.addHeaderView(error_head);
            String info = "";
            if (type == 1){
                info = "您还没有关注别人哦";
            }else if (type == 2){
                info = "目前还没有人关注您";
            }
            ((TextView)error_head.findViewById(R.id.text_view_error)).setText(info);
        }

        wdglFragment.setAdapter(adapter);
    }

    private void doGZOrNOt(int id) {
        HashMap<String,String> map = new HashMap<>();
        map.put("code",HttpUtil.getParam());
        //int id = SampleApplicationLike.getUserloginInstance().getId();
        map.put("id",SampleApplicationLike.getUserloginInstance().getId()+"");
        map.put("quiz_id",id+"");
        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, GZBean.class, URLAddress.URLBASE + URLAddress.URL_GZ, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                GZBean gzBean = (GZBean) response;
                if (gzBean.getResult() == 1){
                    Toasts.show(getActivity(), ""+gzBean.getData().getMsg(), Toast.LENGTH_SHORT);
                }else{
                    Toasts.show(getActivity(), ""+gzBean.getErr_msg(), Toast.LENGTH_SHORT);
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                error.toString();
                Toasts.show(getActivity(), ""+error.toString(), Toast.LENGTH_SHORT);
            }
        });
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        ButterKnife.unbind(this);
    }
}
